<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Loginc {
	
	//判断用户是否登录，如果登录了，就返回true，建议不要调用此函数，可调用checkLogin()，直接实现若用户未登录则跳转到登录页面。
    public function isLogin()
	{
		$CI = & get_instance();
		$logged_in = $CI->session->userdata('logged_in');
		if (isset($logged_in) && $logged_in === TRUE) return true;
		else return false;
	}
	
	//判断用户是否登录，如果没有登录则跳转到其他页面
	public function checkLogin()
	{
		//若要取消登陆，就取消注释下面的return
		//return ;
		if (!$this->isLogin())
		{
			//跳转
			echo '<script>url="/phpdb/index.php/login";window.location.href=url;</script>';
		}
	}
	
	//获取当前登录用户userid
	//session中存储了array('userid' => $username,'logged_in' => TRUE)
	public function getUserId()
	{
		$CI = & get_instance();
		$userid = $CI->session->userdata('userid');
		return $userid;
	}
	
	//登录，仅在登录页面调用
	
	public function login($username,$password)
	{
		$CI = & get_instance();
		$sql = "SELECT * FROM user WHERE User_ID = ".$CI->db->escape($username)." AND Password = MD5(".$CI->db->escape($password).")";
		$query = $CI->db->query($sql);
		if ($query->num_rows()>0) 
		{
			$newdata = array(
						'userid' => $username,
						'logged_in' => TRUE
					);
			$CI->session->set_userdata($newdata);
			return true;
		}
		else
		{
			return false;
		}
	}
	
}

// END Auth class

/* End of file Auth.php */
/* Location: ./php_application/libraries/Pages.php */